﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YJKPretreatmentModeling.SpraySystemReBuild.ReadSpraySystemDWGData.CADPipeExport;
using YJKRevitKernel.FormUtility;
using SpraySystemReBuild;
using SpraySystemReBuild.WinForm;
using YJKPresentation.Utils;

namespace CADTransformRevit
{
	public partial class SpraySystemReBuildForm : System.Windows.Forms.Form
	{
		public SpraySystemReBuildForm(ProjectDefaultOptionData transOptionData, CADData getCADData, UIDocument uiDoc, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.m_transOptionData = transOptionData;
			this.m_CADData = getCADData;
			this.m_doc = doc;
			this.m_uiDoc = uiDoc;
		}

		private void SpraySystemReBuildForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (!CmdSpraySystemReBuild.m_PipeDWGCADData.IsUpperStrEmpty)
			{
				this.gbxUpperSpraySetting.Enabled = false;
			}
			if (!CmdSpraySystemReBuild.m_PipeDWGCADData.IsLowerStrEmpty)
			{
				this.gbxLowerSpraySetting.Enabled = false;
			}
			this.ReadProjectComboBoxAddItems(this.cmbUpperReadProject, this.m_transOptionData.LstSprinklerType1);
			this.ReadProjectComboBoxAddItems(this.cmbLowerReadProject, this.m_transOptionData.LstSprinklerType2);
			this.CommonYJKComboBoxAddItems(1, this.cmbUpperCommonTypes);
			this.CommonYJKComboBoxAddItems(2, this.cmbLowerCommonTypes);
			int selectedIndex = this.ReadPipeTypeComboBoxAddItems(this.cmbPipeType, this.m_transOptionData.LstPipeType);
			this.cmbPipeType.SelectedIndex = selectedIndex;
			int selectedIndex2 = this.ReadPipeSystemTypeComboBoxAddItems(this.cmbPipingSystemType, this.m_transOptionData.LstPipingSystemType);
			this.cmbPipingSystemType.SelectedIndex = selectedIndex2;
			this.AddComboBoxItems(this.cmbMinPipeDiam, this.m_transOptionData.ArMinPipeDiam);
			this.AddControlGroup();
			this.m_ControlInfoManager = new ControlInfoManager(this, this.m_lstControlGroup, null, null);
			if (!this.m_ControlInfoManager.SetControlInfoToForm())
			{
				this.txtUpperFlOffset.Text = "3200";
				this.txtLowerFlOffset.Text = "2400";
				this.textPipeOffset.Text = "2700";
				this.cmbMinPipeDiam.SelectedIndex = 0;
			}
			if (!this.m_CADData.IsUpperStrEmpty)
			{
				this.rdoUpperReadProject.Enabled = false;
				this.cmbUpperReadProject.Enabled = false;
				this.rdoUpperCommonTypes.Enabled = false;
				this.cmbUpperCommonTypes.Enabled = false;
				this.txtUpperFlOffset.Enabled = false;
			}
			else if (this.rdoUpperReadProject.Checked)
			{
				this.cmbUpperReadProject.Enabled = true;
				this.cmbUpperCommonTypes.Enabled = false;
			}
			else
			{
				this.cmbUpperReadProject.Enabled = false;
				this.cmbUpperCommonTypes.Enabled = true;
			}
			if (!this.m_CADData.IsLowerStrEmpty)
			{
				this.rdoLowerReadProject.Enabled = false;
				this.cmbLowerReadProject.Enabled = false;
				this.rdoLowerCommonTypes.Enabled = false;
				this.cmbLowerCommonTypes.Enabled = false;
				this.txtLowerFlOffset.Enabled = false;
			}
			else if (this.rdoLowerReadProject.Checked)
			{
				this.cmbLowerReadProject.Enabled = true;
				this.cmbLowerCommonTypes.Enabled = false;
			}
			else
			{
				this.cmbLowerReadProject.Enabled = false;
				this.cmbLowerCommonTypes.Enabled = true;
			}
			if (this.m_CADData.IsDefineDiameter)
			{
				this.cmbMinPipeDiam.Enabled = false;
			}
		}

		protected override void OnClosed(EventArgs e)
		{
			this.m_ControlInfoManager.GetControlInfoToXml();
		}

		private void ReadProjectComboBoxAddItems(System.Windows.Forms.ComboBox cmb, List<string> lstOptionData)
		{
			foreach (string item in lstOptionData)
			{
				cmb.Items.Add(item);
			}
			if (lstOptionData.Count != 0)
			{
				cmb.SelectedIndex = 0;
			}
		}

		private int ReadPipeTypeComboBoxAddItems(System.Windows.Forms.ComboBox cmb, List<string> lstOptionData)
		{
			if (lstOptionData.Count != 0)
			{
				foreach (string item in lstOptionData)
				{
					cmb.Items.Add(item);
				}
				for (int i = 0; i < cmb.Items.Count; i++)
				{
					if (cmb.Items[i].ToString().Contains("镀锌钢管") && cmb.Items[i].ToString().Contains("丝接与卡箍"))
					{
						return i;
					}
				}
				for (int j = 0; j < cmb.Items.Count; j++)
				{
					if (cmb.Items[j].ToString().Contains("镀锌钢管") && cmb.Items[j].ToString().Contains("卡箍") && !cmb.Items[j].ToString().Contains("丝接"))
					{
						return j;
					}
				}
				for (int k = 0; k < cmb.Items.Count; k++)
				{
					if (cmb.Items[k].ToString().Contains("镀锌钢管") && cmb.Items[k].ToString().Contains("丝接") && !cmb.Items[k].ToString().Contains("卡箍"))
					{
						return k;
					}
				}
			}
			return 0;
		}

		private int ReadPipeSystemTypeComboBoxAddItems(System.Windows.Forms.ComboBox cmb, List<string> lstOptionData)
		{
			if (lstOptionData.Count != 0)
			{
				foreach (string item in lstOptionData)
				{
					cmb.Items.Add(item);
				}
				for (int i = 0; i < cmb.Items.Count; i++)
				{
					if (cmb.Items[i].ToString().Contains("喷淋系统"))
					{
						return i;
					}
				}
				for (int j = 0; j < cmb.Items.Count; j++)
				{
					if (cmb.Items[j].ToString().Contains("喷淋"))
					{
						return j;
					}
				}
				for (int k = 0; k < cmb.Items.Count; k++)
				{
					if (cmb.Items[k].ToString().Contains("自喷"))
					{
						return k;
					}
				}
				for (int l = 0; l < cmb.Items.Count; l++)
				{
					if (cmb.Items[l].ToString().Contains("湿式消防"))
					{
						return l;
					}
				}
			}
			return 0;
		}

		private void AddComboBoxItems(System.Windows.Forms.ComboBox cmb, string[] arOptionData)
		{
			foreach (string item in arOptionData)
			{
				cmb.Items.Add(item);
			}
			if (arOptionData.Count<string>() != 0)
			{
				cmb.SelectedIndex = 0;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.cmbMinPipeDiam.Text == string.Empty || this.cmbMinPipeDiam.Text == null)
			{
				YJKMessageBox.Warning("数值不能为空！");
				return;
			}
			try
			{
				Convert.ToDouble(this.cmbMinPipeDiam.Text);
			}
			catch
			{
				YJKMessageBox.Warning("输入字符串不正确！");
				return;
			}
			if (!this.JudgeSettingValue())
			{
				return;
			}
			base.DialogResult = DialogResult.OK;
		}

		private bool JudgeSettingValue()
		{
			bool flag = true;
			double textValue = this.GetTextValue(this.textPipeOffset, ref flag);
			if (!flag)
			{
				return flag;
			}
			if (textValue <= 0.0)
			{
				YJKMessageBox.Warning("距楼层相对标高不能为负值");
				this.textPipeOffset.Focus();
				this.textPipeOffset.SelectAll();
				flag = false;
				return flag;
			}
			textValue = this.GetTextValue(this.txtUpperFlOffset, ref flag);
			if (!flag)
			{
				return flag;
			}
			if (textValue <= 0.0)
			{
				YJKMessageBox.Warning("距楼层相对标高不能为负值");
				this.txtUpperFlOffset.Focus();
				this.txtUpperFlOffset.SelectAll();
				flag = false;
				return flag;
			}
			textValue = this.GetTextValue(this.txtLowerFlOffset, ref flag);
			if (!flag)
			{
				return flag;
			}
			if (textValue <= 0.0)
			{
				YJKMessageBox.Warning("距楼层相对标高不能为负值");
				this.txtLowerFlOffset.Focus();
				this.txtLowerFlOffset.SelectAll();
				flag = false;
				return flag;
			}
			if (this.m_CADData.IsUpperStrEmpty)
			{
				if (this.rdoUpperReadProject.Checked)
				{
					if (this.cmbUpperReadProject.SelectedItem == null)
					{
						YJKMessageBox.Warning("喷淋的上喷喷头类型不能为空");
						flag = false;
						return flag;
					}
				}
				else if (this.cmbUpperCommonTypes.SelectedItem == null)
				{
					YJKMessageBox.Warning("喷淋的上喷喷头类型不能为空");
					flag = false;
					return flag;
				}
			}
			if (this.m_CADData.IsLowerStrEmpty)
			{
				if (this.rdoUpperReadProject.Checked)
				{
					if (this.cmbLowerReadProject.SelectedItem == null)
					{
						YJKMessageBox.Warning("喷淋的下喷喷头类型不能为空");
						flag = false;
						return flag;
					}
				}
				else if (this.cmbLowerCommonTypes.SelectedItem == null)
				{
					YJKMessageBox.Warning("喷淋的下喷喷头类型不能为空");
					flag = false;
					return flag;
				}
			}
			return flag;
		}

		private double GetTextValue(System.Windows.Forms.TextBox txtBox, ref bool beCheck)
		{
			double result;
			try
			{
				double num = Convert.ToDouble(txtBox.Text);
				beCheck = true;
				result = num;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				YJKMessageBox.Warning("数值不能为空！");
				beCheck = false;
				result = 0.0;
			}
			return result;
		}

		internal FormData GetFormDate()
		{
			FormData formData = new FormData();
			NozzleSetInfo item = new NozzleSetInfo();
			item = this.SetNozzleInfo(NozzleDirectionEnum.eUpper);
			formData.LstNozzleSetInfo.Add(item);
			NozzleSetInfo item2 = new NozzleSetInfo();
			item2 = this.SetNozzleInfo(NozzleDirectionEnum.eLower);
			formData.LstNozzleSetInfo.Add(item2);
			formData.DMiddleOffset = Convert.ToDouble(this.textPipeOffset.Text);
			formData.StrUsePipeType = this.cmbPipeType.SelectedItem.ToString();
			formData.SystemType = this.cmbPipingSystemType.SelectedItem.ToString();
			formData.NCanProducedMinDiam = Convert.ToInt32(this.cmbMinPipeDiam.SelectedItem.ToString());
			formData.BMarkNoConnect = this.chbMarkNOConnect.Checked;
			Level genLevel = this.m_doc.ActiveView.GenLevel;
			formData.CurrentDefaultLevel = genLevel;
			this.m_selePt = this.m_uiDoc.Selection.PickPoint((Autodesk.Revit.UI.Selection.ObjectSnapTypes)512);
			formData.PtLocation = new XYZ(this.m_selePt.X, this.m_selePt.Y, formData.CurrentDefaultLevel.Elevation + Common.MMToFeet(formData.DMiddleOffset));
			TimeSpan startTimeSpan = new TimeSpan(DateTime.Now.Ticks);
			formData.StartTimeSpan = startTimeSpan;
			this.m_formData = formData;
			return formData;
		}

		public void SaveTransformsInfoToXml()
		{
			try
			{
				this.m_xmlDoc = new XmlDocument();
				XmlElement documentElement = this.m_xmlDoc.DocumentElement;
				XmlNode xmlNode = this.m_xmlDoc.CreateElement("CAD转化Revit界面数据");
				this.m_xmlDoc.AppendChild(xmlNode);
				XmlNode xmlRootNode = this.SetxmlSetElementNodeInfo(xmlNode, "消防喷淋喷头设置");
				XmlNode xmlSetInfo = this.SetxmlSetElementNodeInfo(xmlRootNode, "上喷头设置");
				this.SetXmlNodeInfo(xmlSetInfo, "上喷偏移楼层距离", "UpperNozzleOffsetFloor", this.txtUpperFlOffset.Text);
				this.SetXmlNodeInfo(xmlSetInfo, "上喷喷头类型", "UpperNozzleType", this.cmbUpperReadProject.SelectedItem.ToString());
				XmlNode xmlSetInfo2 = this.SetxmlSetElementNodeInfo(xmlNode, "下喷头设置");
				this.m_xmlDoc.CreateElement("是否生成下喷头");
				this.m_xmlDoc.CreateAttribute("BoolUpperNozzle");
				this.SetXmlNodeInfo(xmlSetInfo2, "下喷偏移楼层距离", "LowerNozzleOffsetFloor", this.txtLowerFlOffset.Text);
				this.SetXmlNodeInfo(xmlSetInfo2, "下喷喷头类型", "LowerNozzleType", this.cmbLowerReadProject.SelectedItem.ToString());
				XmlNode xmlSetInfo3 = this.SetxmlSetElementNodeInfo(xmlNode, "管道设置");
				this.SetXmlNodeInfo(xmlSetInfo3, "获取管道中心偏移距离", "PipeOffset", this.textPipeOffset.Text);
				this.SetXmlNodeInfo(xmlSetInfo3, "系统类型", "PipeSystemType", this.cmbPipingSystemType.SelectedItem.ToString());
				this.SetXmlNodeInfo(xmlSetInfo3, "管道类型", "PipeType", this.cmbPipeType.SelectedItem.ToString());
				XmlNode xmlSetInfo4 = this.SetxmlSetElementNodeInfo(xmlNode, "其他");
				this.SetXmlNodeInfo(xmlSetInfo4, "管道所在楼层", "LevelLocation", this.cmbMinPipeDiam.SelectedItem.ToString());
				this.SetBoolXmlInfo(xmlSetInfo4, "是否标明未连接处", "IsMarkNOConnect", this.chbMarkNOConnect.Checked);
				XmlNode newChild = this.SetxmlSetElementNodeInfo(xmlNode, "插入点位置设置");
				xmlNode.AppendChild(newChild);
				this.m_xmlDoc.Save("E:\\2.参与项目\\1.CADTransRevit\\保存界面数据文件.xml");
			}
			catch (Exception)
			{
			}
		}

		private XmlNode SetxmlSetElementNodeInfo(XmlNode xmlRootNode, string strXmlNodeName)
		{
			XmlNode xmlNode = this.m_xmlDoc.CreateElement(strXmlNodeName);
			xmlRootNode.AppendChild(xmlNode);
			return xmlNode;
		}

		private void SetXmlNodeInfo(XmlNode xmlSetInfo, string strXmlNodeName, string strXmlAttributeName, string strXmlAttributeValue)
		{
			XmlNode xmlNode = this.m_xmlDoc.CreateElement(strXmlNodeName);
			XmlAttribute xmlAttribute = this.m_xmlDoc.CreateAttribute(strXmlAttributeName);
			xmlAttribute.InnerText = strXmlAttributeValue;
			xmlSetInfo.AppendChild(xmlNode);
			xmlNode.Attributes.Append(xmlAttribute);
		}

		private void SetBoolXmlInfo(XmlNode xmlSetInfo, string strXmlNodeName, string strXmlAttributeName, bool isCreate)
		{
			XmlNode xmlNode = this.m_xmlDoc.CreateElement(strXmlNodeName);
			XmlAttribute xmlAttribute = this.m_xmlDoc.CreateAttribute(strXmlAttributeName);
			if (isCreate)
			{
				xmlAttribute.InnerText = "是";
				xmlSetInfo.AppendChild(xmlNode);
				xmlNode.Attributes.Append(xmlAttribute);
				return;
			}
			xmlAttribute.InnerText = "否";
			xmlSetInfo.AppendChild(xmlNode);
			xmlNode.Attributes.Append(xmlAttribute);
		}

		private bool IsEmpty(string strText)
		{
			bool result = true;
			if (strText != string.Empty)
			{
				result = false;
			}
			return result;
		}

		private NozzleSetInfo SetNozzleInfo(NozzleDirectionEnum nozzleDirEnum)
		{
			NozzleSetInfo nozzleSetInfo = new NozzleSetInfo();
			if (NozzleDirectionEnum.eUpper == nozzleDirEnum)
			{
				nozzleSetInfo.DNozzleOffsetFloor = Convert.ToDouble(this.txtUpperFlOffset.Text);
				nozzleSetInfo.Symbol = nozzleDirEnum;
				if (this.rdoUpperReadProject.Checked)
				{
					if (this.cmbUpperReadProject.SelectedItem != null)
					{
						nozzleSetInfo.StrNozzleReadProjectType = this.cmbUpperReadProject.SelectedItem.ToString();
					}
				}
				else if (this.cmbUpperCommonTypes.SelectedItem != null)
				{
					nozzleSetInfo.StrNozzleCommonHYType = this.cmbUpperCommonTypes.SelectedItem.ToString();
				}
			}
			if (NozzleDirectionEnum.eLower == nozzleDirEnum)
			{
				nozzleSetInfo.DNozzleOffsetFloor = Convert.ToDouble(this.txtLowerFlOffset.Text);
				nozzleSetInfo.Symbol = nozzleDirEnum;
				if (this.rdoUpperReadProject.Checked)
				{
					if (this.cmbLowerReadProject.SelectedItem != null)
					{
						nozzleSetInfo.StrNozzleReadProjectType = this.cmbLowerReadProject.SelectedItem.ToString();
					}
				}
				else if (this.cmbLowerCommonTypes.SelectedItem != null)
				{
					nozzleSetInfo.StrNozzleCommonHYType = this.cmbLowerCommonTypes.SelectedItem.ToString();
				}
			}
			return nozzleSetInfo;
		}

		private static void EnterNumbersOnly(KeyPressEventArgs e)
		{
			if (e.KeyChar < '0' || e.KeyChar > '9')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' || e.KeyChar == '\b')
			{
				e.Handled = false;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void rdoUpperReadProject_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoUpperReadProject.Checked)
			{
				this.cmbUpperReadProject.Enabled = true;
				this.cmbUpperCommonTypes.Enabled = false;
			}
		}

		private void rdoUpperCommonTypes_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoUpperCommonTypes.Checked)
			{
				this.cmbUpperReadProject.Enabled = false;
				this.cmbUpperCommonTypes.Enabled = true;
			}
		}

		private void rdoLowerReadProject_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLowerReadProject.Checked)
			{
				this.cmbLowerReadProject.Enabled = true;
				this.cmbLowerCommonTypes.Enabled = false;
			}
		}

		private void rdoLowerCommonTypes_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLowerCommonTypes.Checked)
			{
				this.cmbLowerCommonTypes.Enabled = true;
				this.cmbLowerReadProject.Enabled = false;
			}
		}

		private void CommonYJKComboBoxAddItems(int nFamilyCategory, System.Windows.Forms.ComboBox thisComboBox)
		{
			List<ListItem> allName = this.m_spaceFamilyDataReader.GetAllName("Sprinkler", nFamilyCategory);
			if (allName.Count > 0)
			{
				foreach (ListItem listItem in allName)
				{
					int id = int.Parse(listItem.Tag.ToString());
					foreach (ListItem listItem2 in this.m_spaceFamilyDataReader.GetInfoById("Sprinkler", id))
					{
						if (listItem2.Name == "FamilySymbol")
						{
							this.GetFamilySybolNameAddItems(thisComboBox, listItem2.Value, listItem.Name);
							break;
						}
					}
				}
			}
			if (thisComboBox.Items.Count != 0)
			{
				thisComboBox.SelectedIndex = 0;
			}
		}

		private void GetFamilySybolNameAddItems(System.Windows.Forms.ComboBox thisComboBox, string strSybolNameData, string strSprinkFamilyName)
		{
			foreach (string text in strSybolNameData.Split(new char[]
			{
				';',
				',',
				'，'
			}))
			{
				if (text.Trim() != "")
				{
					string item = strSprinkFamilyName + "-" + text;
					thisComboBox.Items.Add(item);
				}
			}
		}

		private void AddControlGroup()
		{
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.rdoUpperReadProject, false));
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.rdoUpperCommonTypes, false));
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.txtUpperFlOffset, false));
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.rdoLowerReadProject, false));
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.rdoLowerCommonTypes, false));
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.txtLowerFlOffset, false));
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.textPipeOffset, false));
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.chbMarkNOConnect, false));
			this.m_lstControlGroup.Add(new KeyValuePair<System.Windows.Forms.Control, bool>(this.cmbMinPipeDiam, false));
		}

		private void SpraySystemReBuildForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private SpaceFamilyDataReader m_spaceFamilyDataReader = new SpaceFamilyDataReader();

		private ProjectDefaultOptionData m_transOptionData;

		private CADData m_CADData;

		private ControlInfoManager m_ControlInfoManager;

		private List<KeyValuePair<System.Windows.Forms.Control, bool>> m_lstControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();

		private UIDocument m_uiDoc;

		private Document m_doc;

		private XYZ m_selePt;

		private FormData m_formData;

		private XmlDocument m_xmlDoc;
	}
}
